package com.TCP;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @ClassName SocketDemo
 * @Description TCP多发多收（因单线程，不能处理多个客户端）
 * @Author 2214361854
 * @Date 2022/3/3 22:37
 * @Version 1.0
 */
public class SocketDemo2 {
//    public static void main(String[] args) throws Exception {
//        System.out.println("====服务端启动====");
//        //服务端，创建ServerSocket
//        final ServerSocket serverSocket = new ServerSocket(7777);
//        //接收连接请求
//        final Socket socket = serverSocket.accept();
//        //获取并包装缓冲字符流
//        final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        //读取消息
//        String msg;
//        while ((msg = br.readLine()) != null) {
//            System.out.println(socket.getRemoteSocketAddress()+"说了："+msg);
//        }
//
//
////        关闭
////        serverSocket.close();
//    }
//}
//
///**
// * 客户端
// */
//class ClientDemo2{
//    public static void main(String[] args) throws Exception {
//        System.out.println("====客户端启动====");
//        //客户端，创建Soket
//        final Socket sk = new Socket("127.0.0.1",7777);
//        //获取并包装成字节打印流
//        final PrintStream ps = new PrintStream(sk.getOutputStream());
//        //发送消息并刷新流
//        final Scanner scanner = new Scanner(System.in);
//        while (true) {
//            System.out.println("TCP客户端：");
//            String s = scanner.nextLine();
//            ps.println(s);
//            ps.flush();
//        }
////        关闭
////        sk.close();
//    }
}